package com.huoshan.im.server.config;

import online.chazi.bootplus.multiple.redis.MultipleRedisManager;
import online.chazi.bootplus.multiple.redis.MultipleRedisTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * 说明：作者很懒，什么都没留下
 * Created by 火山九日 on 2020-03-04 09:19
 */
@Configuration
public class RedisConfig {

    @Resource
    private MultipleRedisManager<String, Object> multipleRedisManager;

    //默认的redis
    @Bean
    RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        return buildRedisTemplate(redisConnectionFactory);
    }

    @Bean
    MultipleRedisTemplateBuilder<String, Object> multipleRedisTemplateBuilder() {
        return this::buildRedisTemplate;
    }

    private RedisTemplate<String, Object> buildRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

    @Bean
    RedisTemplate<String, Object> userServerMapping1RedisTemplate() {
        return multipleRedisManager.getRedisTemplate("user-server-mapping1");
    }

    @Bean
    RedisTemplate<String, Object> userServerMapping2RedisTemplate() {
        return multipleRedisManager.getRedisTemplate("user-server-mapping2");
    }

    @Bean
    RedisTemplate<String, Object> userServerMapping3RedisTemplate() {
        return multipleRedisManager.getRedisTemplate("user-server-mapping3");
    }

    //======================
    @Bean
    RedisTemplate<String, Object> user1RedisTemplate() {
        return multipleRedisManager.getRedisTemplate("user1");
    }

    @Bean
    RedisTemplate<String, Object> user2RedisTemplate() {
        return multipleRedisManager.getRedisTemplate("user2");
    }

    @Bean
    RedisTemplate<String, Object> user3RedisTemplate() {
        return multipleRedisManager.getRedisTemplate("user3");
    }


}
